package com.dgshanger.sanhxiaofeisc.common;

import android.text.TextUtils;

import com.blankj.utilcode.util.SPUtils;
import com.dgshanger.sanhxiaofeisc.entity.HomeEntity;
import com.dgshanger.sanhxiaofeisc.entity.LoginEntity;
import com.dgshanger.sanhxiaofeisc.entity.NearbyItemEntity;
import com.dgshanger.sanhxiaofeisc.entity.UserEntity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xx.baseutilslibrary.network.retrofit.Retrofit2Manager;

import java.util.ArrayList;
import java.util.List;

/**
 * Constants
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/2/26 10:55.
 */

public class Constants {

    public static final String KEY_SP_TOKEN = "SP_TOKEN";
    public static final String KEY_INTENT_MAIN = "INTENT_MAIN";
    public static final String KEY_INTENT_VIP = "VipID";//Vip详情页传递字段
    public static final String KEY_INTENT_VIP_COUNT = "vipid_count";//Vip详情页传递字段
    public static final String KEY_INTENT_MSG_ID = "msg_id";//消息id
    public static final String KEY_INTENT_EXTRA_ID = "extra_id";//消息id
    public static final String KEY_INTENT_LOG_ID = "log_id";//订单记录id
    public static final String KEY_DATA = "data";//Intent数据传输字段
    public static final String KEY_INTENT_TYPE = "type";//Intent数据传输字段
    //滑动关闭Activity
    public static final int ACTIVITY_FINISH_REQUEST_CODE = 10000;
    public static final int ACTIVITY_FINISH_RESULT_CODE = 10001;
    //接口Code
    public static final String LONG_TOKEN_INVALID = "30001";
    public static final String LONG_TOKEN_INVALID_ = "30002";
    public static final String SHORT_TOKEN_INVALID = "30003";
    public static final String SHORT_TOKEN_INVALID_ = "30004";
    public static final String EXTRA_BUNDLE = "EXTRA_BUNDLE";
    private static final String KEY_SP_USER = "SP_USER";
    private static final String KEY_SP_LOCATION = "SP_LOCATION";//定位信息
    private static final String KEY_SP_IS_FIRST = "SP_IS_FIRST";//是否第一次使用当前版本App
    private static final String KEY_SP_HOME = "SP_HOME";//首页数据
    private static final String KEY_SP_IS_FIRST_LOGIN = "SP_IS_FIRST_LOGIN";//是否第一登录
    private static final String KEY_SP_IS_FIRST_NEARBY = "SP_IS_FIRST_NEARBY";//是否第一次进入附近
    private static final String KEY_SP_HISTORY_SEARCH = "SP_HISTORY_SEARCH";//历史记录
    private static final String KEY_SP_PAY_TOKEN = "SP_PAY_TOKEN";//pay_token
    private static final String KEY_SP_NEARBY = "SP_NEARBY";//附近数据
    private static final String KEY_UNREAD_MESSAGE = "UNREAD_MESSAGE";//未读消息

    static {
        System.loadLibrary("native-lib");
    }

    public static native String getMysteriousNum();//生成二维码的神秘数组

    /**
     * 用户是否登录
     */
    public static boolean isLogin() {
        return !(getToken() == null || TextUtils.isEmpty(getToken().getSign_api()));
    }

    /**
     * 是否是第一次使用
     */
    public static boolean isFirst() {
        return SPUtils.getInstance().getBoolean(KEY_SP_IS_FIRST, true);
    }

    /**
     * 设置为非首次使用app
     */
    public static void setNotFirst() {
        SPUtils.getInstance().put(KEY_SP_IS_FIRST, false);
    }

    /**
     * 获取token
     *
     * @return
     */
    public static LoginEntity.TokenBean getToken() {
        String tokenJson = SPUtils.getInstance().getString(KEY_SP_TOKEN);
        return new Gson().fromJson(tokenJson, LoginEntity.TokenBean.class);
    }

    /**
     * 获取tokenSignApi
     */
    public static String getTokenSignApi() {
        if (isLogin()) {
            String loginJson = SPUtils.getInstance().getString(KEY_SP_TOKEN);
            return new Gson().fromJson(loginJson, LoginEntity.TokenBean.class).getSign_api();
        } else {
            return "";
        }
    }

    /**
     * 储存token
     *
     * @param tokenBean
     */
    public static void putToken(LoginEntity.TokenBean tokenBean) {
        SPUtils.getInstance().put(KEY_SP_TOKEN, new Gson().toJson(tokenBean));
    }

    public static ArrayList<String> getHistorySearch() {
        String historyJson = SPUtils.getInstance().getString(KEY_SP_HISTORY_SEARCH);
        return new Gson().fromJson(historyJson, new TypeToken<ArrayList<String>>() {
        }.getType());
    }

    /**
     * 储存token
     *
     * @param userEntity
     */
    public static void putUserEntity(UserEntity userEntity) {
        SPUtils.getInstance().put(KEY_SP_USER, new Gson().toJson(userEntity));
    }

    /**
     * 获取用户信息
     */
    public static UserEntity getUserEntity() {
        String userJson = SPUtils.getInstance().getString(KEY_SP_USER);
        return new Gson().fromJson(userJson, UserEntity.class);
    }

    /**
     * 储存历史搜索记录
     *
     * @param historyList
     */
    public static void putHistorySearch(List<String> historyList) {
        SPUtils.getInstance().put(KEY_SP_HISTORY_SEARCH, new Gson().toJson(historyList));
    }

    /**
     * 获取是否第一次登录
     *
     * @return
     */
    public static boolean isFirstLogin() {
        return SPUtils.getInstance().getBoolean(KEY_SP_IS_FIRST_LOGIN, true);
    }

    /**
     * 储存是否第一次登录
     *
     * @param isFirstLogin
     */
    public static void setIsFirstLogin(boolean isFirstLogin) {
        SPUtils.getInstance().put(KEY_SP_IS_FIRST_LOGIN, isFirstLogin);
    }

    /**
     * 获取是否第一次进入附近
     *
     * @return
     */
    public static boolean isFirstNearby() {
        return SPUtils.getInstance().getBoolean(KEY_SP_IS_FIRST_NEARBY, true);
    }

    /**
     * 储存是否第一次进入附近
     *
     * @param isFirstNearby
     */
    public static void setIsFirstNearby(boolean isFirstNearby) {
        SPUtils.getInstance().put(KEY_SP_IS_FIRST_NEARBY, isFirstNearby);
    }

    /***
     * 获取首页数据
     * @return
     */
    public static HomeEntity getHomeData() {
        String homeJson = SPUtils.getInstance().getString(KEY_SP_HOME);
        return new Gson().fromJson(homeJson, HomeEntity.class);
    }

    /**
     * 储存首页数据
     *
     * @param homeEntity
     */
    public static void putHomeData(HomeEntity homeEntity) {
        SPUtils.getInstance().put(KEY_SP_HOME, new Gson().toJson(homeEntity));
    }

    /**
     * 获取附近数据
     *
     * @return
     */
    public static List<NearbyItemEntity> getNearbyData() {
        String nearbyJson = SPUtils.getInstance().getString(KEY_SP_NEARBY);
        return new Gson().fromJson(nearbyJson, new TypeToken<List<NearbyItemEntity>>() {
        }.getType());
    }

    /**
     * 储存附近数据
     *
     * @param nearbyData
     */
    public static void putNearbyData(List<NearbyItemEntity> nearbyData) {
        SPUtils.getInstance().put(KEY_SP_NEARBY, new Gson().toJson(nearbyData));
    }

    /**
     * 获取ay
     *
     * @return
     */
    public static String getPayToken() {
        return SPUtils.getInstance().getString(KEY_SP_PAY_TOKEN);
    }

    public static void putPayToken(String payToken) {
        SPUtils.getInstance().put(KEY_SP_PAY_TOKEN, payToken);
    }

    /**
     * 获取Host
     *
     * @return
     */
    public static String getBaseUrl() {
        return Retrofit2Manager.Companion.getInstance().getApiConfigProvider().getHost();
    }

    /**
     * 存入经纬信息
     */
    public static void putLocation(double latitude, double longitude, String city) {
        SPUtils.getInstance().put(Constants.KEY_SP_LOCATION, latitude + "," + longitude + "," + city);
    }

    /**
     * 获取经纬信息
     *
     * @return
     */
    public static String[] getLocation() {
        String location = SPUtils.getInstance()
                .getString(Constants.KEY_SP_LOCATION);
        if (TextUtils.isEmpty(location)) {
            return new String[]{"39.92", "116.46", "北京"};
        } else {
            return location
                    .split(",");
        }

    }

    /**
     * 添加未读消息数量
     */
    public static void addUnReadMessage() {
        SPUtils.getInstance().put(KEY_UNREAD_MESSAGE, getUnReadMessage() + 1);
    }

    /**
     * 获取未读消息数量
     */
    public static int getUnReadMessage() {
        return SPUtils.getInstance().getInt(KEY_UNREAD_MESSAGE, 0);
    }

    /**
     * 清楚消息记录
     */
    public static void cleanMessage() {
        SPUtils.getInstance().put(KEY_UNREAD_MESSAGE, 0);
    }

}
